package com.purong.purong.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.purong.purong.R;
import com.purong.purong.bean.BankLoanBean;
import com.purong.purong.util.CommonUtil;
import com.yao.sdk.glide.GlideUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wenjiezeng on 2017/10/31.
 */
public class BankLoanAdapter extends BaseAdapter {
    private Context mContext;
    private List<BankLoanBean> loanList = new ArrayList<>();
    public BankLoanAdapter(Context context, List<BankLoanBean> list){
        this.mContext=context;
        this.loanList=list;
    }
    @Override
    public int getCount() {
        return loanList.size();
    }

    @Override
    public Object getItem(int position) {
        return loanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder mHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.bank_loan_item, null);
            mHolder = new ViewHolder(convertView);
            convertView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }
        GlideUtil.getInstance().load(mContext,loanList.get(position).getLoan_product_url(),mHolder.img,true);
        mHolder.name.setText(loanList.get(position).getLoan_product_name());
        mHolder.rate.setText(loanList.get(position).getTotal_rate()+"%/月");
        mHolder.note.setText(loanList.get(position).getLoan_product_note());
        mHolder.applyNum.setText(CommonUtil.numConvert(Integer.valueOf(loanList.get(position).getLoan_apply_num()),1)+"人申请");
        mHolder.tagOne.setVisibility(View.GONE);
        mHolder.tagTwo.setVisibility(View.GONE);
        mHolder.tagThree.setVisibility(View.GONE);
        for (int i = 0; i <loanList.get(position).getLoan_tag().size() ; i++) {
            if (i==0){
                mHolder.tagOne.setVisibility(View.VISIBLE);
                mHolder.tagOne.setText(loanList.get(position).getLoan_tag().get(0));
            }else if (i==1){
                mHolder.tagTwo.setVisibility(View.VISIBLE);
                mHolder.tagTwo.setText(loanList.get(position).getLoan_tag().get(1));
            }else if (i==2){
                mHolder.tagThree.setVisibility(View.VISIBLE);
                mHolder.tagThree.setText(loanList.get(position).getLoan_tag().get(2));
            }
        }
        if (position!=loanList.size()-1){
            mHolder.cutLine.setVisibility(View.VISIBLE);
        }else {
            mHolder.cutLine.setVisibility(View.GONE);

        }
        return convertView;
    }
     class ViewHolder{
         private ImageView img;
         private LinearLayout mainLay;
         private TextView name,applyNum,note,rate,tagOne,tagTwo,tagThree;
         private View cutLine;
        public ViewHolder(View v) {
            img=(ImageView)v.findViewById(R.id.img);
            name=(TextView)v.findViewById(R.id.name);
            applyNum=(TextView)v.findViewById(R.id.apply_num);
            note=(TextView)v.findViewById(R.id.note);
            rate=(TextView)v.findViewById(R.id.total_rate);
            cutLine=(View)v.findViewById(R.id.cut_line);
            mainLay=(LinearLayout)v.findViewById(R.id.main_lay);
            tagOne=(TextView)v.findViewById(R.id.tag_one);
            tagTwo=(TextView)v.findViewById(R.id.tag_two);
            tagThree=(TextView)v.findViewById(R.id.tag_three);

        }
    }
}
